/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;

import BE.Seccion;
import java.util.ArrayList;
import java.util.List;
import  org.hibernate.Query;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
/**
 *
 * @author CarLita
 */
public class SeccionDao {
    Session session;

    public List buscarPorIdPlantilla(Integer id) {
        session = HibernateUtil.getSessionFactory().openSession();

        String hql = "from Seccion seccion where seccion.plantillaPapelTrabajo.idPlantillaPt= ? ";
        Query query = session.createQuery(hql);
        query.setInteger(0, id);
        List resultList = null;        

        try {
            session.beginTransaction();
            resultList = query.list();
            session.getTransaction().commit();
            session.close();
            return resultList;
        } catch (HibernateException he) {
            he.printStackTrace();            
        }

        session.close();
        return null;        
    }

    public Seccion buscarPorId(int idSeccion){
        session = HibernateUtil.getSessionFactory().openSession();
        String hql = "from Seccion seccion where seccion.idSeccion= ? ";
        Query query = session.createQuery(hql);
        query.setInteger(0, idSeccion);
        List resultList = null;

        try {
            session.beginTransaction();
            resultList = query.list();
            session.getTransaction().commit();
            session.close();
            return (Seccion)resultList.get(0);
        } catch (HibernateException he) {
            he.printStackTrace();
        }

        session.close();
        return null;
    }

    public int grabar(Seccion seccion) {        
        Object o=null; 
        session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        try {
            tx.begin();
            o = session.merge(seccion);
            tx.commit();
        } catch (HibernateException he){
            tx.rollback();
            he.printStackTrace();            
        } finally {
            session.close();
        }
        
        seccion = (Seccion)o;
        System.out.println("Seccion grabada:"+seccion.getIdSeccion());
        return o==null? -1:((Seccion)o).getIdSeccion();
    }

    public boolean eliminar(Seccion seccion) {
        boolean ok =true;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            Transaction tx = session.beginTransaction();
            session.delete(seccion);
            tx.commit();
            session.close();
        }
        catch(Exception e){
            ok = false;
            System.out.println(e.getMessage());
        }
        return ok;
    }

    public List<Seccion> buscarNivelesPorPlantilla(int idPlantilla, int nivel, int secSuperior) {
        session = HibernateUtil.getSessionFactory().openSession();        
               
        String hql = "from Seccion seccion where seccion.plantillaPapelTrabajo.idPlantillaPt= ? "
                + "and seccion.nivelSeccion = ?";
        if (nivel==2){
            hql+= " and seccion.idseccionSuperior1 = ?";
        }
        if (nivel==3){
            hql+= " and seccion.idseccionSuperior2 = ?";
        }
        Query query = session.createQuery(hql);
        query.setInteger(0, idPlantilla);
        query.setInteger(1, nivel);
        if (nivel>1){
            query.setInteger(2, secSuperior);
        }
        List resultList = null;

        try {
            session.beginTransaction();
            resultList = query.list();
            session.getTransaction().commit();
            session.close();
            return resultList;
        } catch (HibernateException he) {
            he.printStackTrace();
        }

        session.close();
        return null;
    }

    

}
